<?php defined('SYSPATH') or die('No direct script access.');
// Load the core Kohana class
require SYSPATH . 'classes/kohana/core' . EXT;
if (is_file(APPPATH . 'classes/kohana' . EXT))
{
    // Application extends the core
    require APPPATH . 'classes/kohana' . EXT;
}
else
{
    // Load empty core extension
    require SYSPATH . 'classes/kohana' . EXT;
}
/**
 * Set the default time zone.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/timezones
 */
//date_default_timezone_set('America/Chicago');
date_default_timezone_set('Asia/Shanghai');

/**
 * Set the default locale.
 *
 * @link http://kohanaframework.org/guide/using.configuration
 * @link http://www.php.net/manual/function.setlocale
 */
setlocale(LC_ALL, 'en_US.utf-8');

/**
 * Enable the Kohana auto-loader.
 *
 * @link http://kohanaframework.org/guide/using.autoloading
 * @link http://www.php.net/manual/function.spl-autoload-register
 */
spl_autoload_register(array('Kohana', 'auto_load'));

/**
 * Optionally, you can enable a compatibility auto-loader for use with
 * older modules that have not been updated for PSR-0.
 *
 * It is recommended to not enable this unless absolutely necessary.
 */
//spl_autoload_register(array('Kohana', 'auto_load_lowercase'));

/**
 * Enable the Kohana auto-loader for unserialization.
 *
 * @link http://www.php.net/manual/function.spl-autoload-call
 * @link http://www.php.net/manual/var.configuration#unserialize-callback-func
 */
ini_set('unserialize_callback_func', 'spl_autoload_call');

/**
 * Set the mb_substitute_character to "none"
 *
 * @link http://www.php.net/manual/function.mb-substitute-character.php
 */
mb_substitute_character('none');

// -- Configuration and initialization -----------------------------------------

/**
 * Set the default language
 */
I18n::lang('ch'); //读取语言包
if (isset($_SERVER['SERVER_PROTOCOL']))
{
    // Replace the default protocol.
    HTTP::$protocol = $_SERVER['SERVER_PROTOCOL'];
}

Kohana::$environment = DEVELOPMENT_MODE ? Kohana::DEVELOPMENT : Kohana::PRODUCTION;
/**
 * Initialize Kohana, setting the default options.
 *
 * The following options are available:
 *
 * - string   base_url    path, and optionally domain, of your application   NULL
 * - string   index_file  name of your index file, usually "index.php"       index.php
 * - string   charset     internal character set used for input and output   utf-8
 * - string   cache_dir   set the internal cache directory                   APPPATH/cache
 * - integer  cache_life  lifetime, in seconds, of items cached              60
 * - boolean  errors      enable or disable error handling                   TRUE
 * - boolean  profile     enable or disable internal profiling               TRUE
 * - boolean  caching     enable or disable internal caching                 FALSE
 * - boolean  expose      set the X-Powered-By header                        FALSE
 */
//后台系统变量指定
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN')
{
    $ar = explode('\\', dirname(dirname(__FILE__)));
    $cfg_backdir = $ar[count($ar) - 1];
}
else
{
    $ar = explode('/', dirname(dirname(__FILE__)));
    $cfg_backdir = $ar[count($ar) - 1];
}

//缓存目录,日志目录指定
$cache_dir = CACHE_DIR.'newtravel';
$logs_dir = LOGS_DIR.'newtravel';
if(!file_exists($cache_dir))
{
    if (!mkdir($cache_dir, 0755, true) && !is_dir($cache_dir)) {
        throw new \RuntimeException(sprintf('Directory "%s" was not created', $cache_dir));
    }
}
if(!file_exists($logs_dir))
{
    if (!mkdir($logs_dir, 0755, true) && !is_dir($logs_dir)) {
        throw new \RuntimeException(sprintf('Directory "%s" was not created', $logs_dir));
    }
}
Kohana::init(array(
    'base_url' => '/' . $cfg_backdir,
    'index_file' => '',
	'errors'=> DEVELOPMENT_MODE,
    'cache_dir' => $cache_dir
));


/**
 * Attach the file write to logging. Multiple writers are supported.
 */
Kohana::$log->attach(new Log_File($logs_dir));
Kohana::$log_errors = true;
/**
 * Attach a file reader to config. Multiple readers are supported.
 */
Kohana::$config->attach(new Config_File);

/**
 * Autoload composer libraries
 */
require BASEPATH . '/vendor/autoload.php';

$default_modules = array(
    'developer' => BASEPATH.'/plugins/developer',
    'database' => MODPATH . 'database',   // Database access
    'image' => MODPATH . 'image',      // Image manipulation
    'orm' => MODPATH . 'orm',       // Object Relationship Mapping
    'pagination' => MODPATH . 'pagination', //分页
    'captcha' => MODPATH . 'captcha',//验证码类
    'cache' => MODPATH . 'cache',
    'cron' => MODPATH .'cron',//任务类
    'hooks' => MODPATH.'hooks',//事件类
    'resque' => MODPATH.'resque',//异步队列
    //'profilertoolbar' => MODPATH.'profilertoolbar'
    //'userguide'  => MODPATH.'userguide' // User guide and API documentation
);
$plugins_modules = include(BASEPATH . '/data/module.php');
foreach ($plugins_modules as $plugin_key => $plugin)
{
    $plugins_modules[$plugin_key] = BASEPATH . $plugin;
}
$default_modules = is_array($plugins_modules) ? array_merge($default_modules, $plugins_modules) : $default_modules;
Kohana::modules($default_modules);

/**
 * Set the routes. Each route must have a minimum of a name, a URI and a set of
 * defaults for the URI.
 */
Route::set('default', '(<controller>(/<action>(/<params>)))', array('params' => '.*'))
    ->defaults(array(
        'controller' => 'index',
        'action' => 'index',
    ));
$cfg_basehost = St_Functions::get_http_prefix() . $_SERVER['HTTP_HOST']; //网站域名
$cfg_cmspath = '/' . $cfg_backdir . '/';
$cfg_public_url = '/' . $cfg_backdir . '/public/';
Cookie::$httponly = true;
Cookie::$salt = 'stourwebcms';
Cookie::$path = "/";
Cookie::$domain = St_Functions::cookie_domain();
//事件加载
Hook::load();
//任务初始化
St_Task::init();
//新增php版本的变化检测
Common::php_version_check();

$weburl = St_Functions::get_main_host();
if(St_Functions::is_SSL() && stripos($weburl,"https")===false){
    $weburl=str_replace("http","https",$weburl);
    DB::update('weblist')
        ->where('webid', '=', 0)
        ->set(array('weburl' => $weburl))
        ->execute();
}

